package com.service;

import com.dao.GoodsDao;
import com.entity.Goods;
import com.entity.Tops;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author liuronghua
 * @version 1.0
 * @data 2020-09-2020/9/23 18:37
 * @desc Goods Service
 */
@Service
public class GoodService {



    @Autowired
    private GoodsDao goodDao;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TopService topService;


    public List<Goods> getListByType(int typeId,int page,int size){
        return typeId > 0 ? packGood(goodDao.selectByTypeId(
                typeId,size * (page-1),size)) : this.getList(page,size);
    }

    /**
     * 封装商品
     * @param list
     * @return
     */
    private List<Goods> packGood(List<Goods> list){
        for (Goods good : list) {
            good =packGood(good);
        }
        return list;
    }

    private Goods packGood(Goods good){
        if (good !=null){
            good.setType(typeService.get(good.getTypeId()));
            good.setTop(Objects.nonNull(topService.getByGoodIdAndType(good.getId(),Tops.TYPE_TODAY)));
        }
        return good;
    }

    public long getCountByType(int typeId){
        return typeId > 0 ? goodDao.selectCountByTypeId(typeId) :   this.getCount();
    }

    public long getCount(){
        return goodDao.selectCount();
    }

    public List<Goods> getList(int page,int size){return goodDao.selectList();}

    public List<Goods> getListAll(){
        return goodDao.selectList();
    }

    public long getCountByTopType(byte type){
        return goodDao.selectCountByTopType(type);
    }

    public List<Goods> getListByTopType(byte type,int page,int size){
        return packGood(goodDao.selectListByTopType(type,size * (page-1),size));
    }

    public List<Goods> getListOrderSales(int page, int size) {
        return packGood(goodDao.selectListOrderSales(size*(page-1),size));
    }

    public List<Goods> getListOrderNew(int page, int size) {
        return packGood(goodDao.selectListOrderNew(size*(page-1),size));
    }

    public Goods getName(String name){
        return packGood(goodDao.selectName(name));
    }

    public Goods getid(int id){
        return packGood(goodDao.select(id));
    }



//    public Goods get(int id){
//        return packGood(goodDao.select(id));
//    }

    /**
     * 修改库存
     * @param id
     * @param stock
     * @return
     */
    public boolean updateStock(int id,int stock){
        return goodDao.updateStock(id,stock);
    }

    /**
     * 修改销量
     * @param id
     * @param sales
     * @return
     */
    public boolean updateSales(int id,int sales){
        return goodDao.updateSales(id,sales);
    }





}
